package com.drool.mediaplayer;

import java.util.ArrayList;

import com.drool.mediaplayer.Adapters.GalleryAlbumAdapter;
import com.drool.mediaplayer.Adapters.SongBrowserAdapter;
import com.drool.mediaplayer.Database.MediaFilesDB;
import com.drool.mediaplayer.Database.MediaFilesDBHelper;
import com.drool.mediaplayer.Database.WritableDatabase;
import com.drool.mediaplayer.Database.MediaFilesDB.PlaylistItems;
import com.drool.mediaplayer.Extracter.AudioInfoExtracter;
import com.drool.mediaplayer.Extracter.VideoInfoExtracter;
import com.drool.mediaplayer.Interfaces.INowPlayingCallback;
import com.drool.mediaplayer.panels.OpaquePanel;
import com.drool.mediaplayer.panels.TranslucentPanel;
import com.drool.mediaplayer.services.DroolService;
import com.drool.mediaplayer.services.IRemoteInterface;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.provider.MediaStore.Audio;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.FilterQueryProvider;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class SongAlbumBrowser extends Activity {

	Bundle extras;
	//Intentextras keys defined by this class
	public static final String SELECTION = "Selection";
	public static final int SELECTION_ALL = 0;
	public static final int SELECTION_ONE = 1;
	
	public static final String DATA = "Data";
	public static final String DATA1 = "Data1";
	private static final String TAG = "AlbumBrows";
	
	private IRemoteInterface mService = null;
	private boolean connected = false;
	private ServiceConnection mConnection = null;
	private int cur_pos =-1;
	Playlist pl;
	
	private INowPlayingCallback npCallback = new INowPlayingCallback(){

		@Override
		public void mpPlaying() throws RemoteException {
			Log.i(TAG, "MP plaing a new song");
			
		}

		@Override
		public IBinder asBinder() {
			// TODO Auto-generated method stub
			return null;
		}
		
	};
	
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		
		Log.i(TAG, "Calling onstart()");
		super.onStart();
		
		ListView lv;
		
		lv = ((ListView)findViewById(R.id.albumListView01));
		
		SongBrowserAdapter ad = (SongBrowserAdapter) lv.getAdapter();
		
		SharedPreferences settings = getSharedPreferences(MyPreferences.PREFERENCE_NAME,0);
		String currentSong = settings.getString(MyPreferences.CURRENT_SONG_ID, "-1");
		
		ad.setCurrentSong(currentSong);
		
		ad.notifyDataSetChanged();		
		
		setupNowPlaying();
		
	}
	
	
	
	
	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
		Log.d("Sudeep", "Shuting down activity");
	}




	void setupNowPlaying(){
		SharedPreferences settings = getSharedPreferences(MyPreferences.PREFERENCE_NAME,0);
		final String currentSong = settings.getString(MyPreferences.CURRENT_SONG_ID, "-1");
		
		
		Handler mHandler = new Handler();
		
		 String mpState = settings.getString(MyPreferences.MPSTATE, null);
		
		final Button but = (Button)findViewById(R.id.NowPlayingBut);
		
		if (mpState.equals(MyPreferences.MPSTATE_VALUE_PLAYING)){
			but.setText("Pause");
		}else but.setText("Play");
		
		
		
		but.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				SharedPreferences settings = getSharedPreferences(MyPreferences.PREFERENCE_NAME,0);
				
				final String mpState = settings.getString(MyPreferences.MPSTATE, null);
				
				if (currentSong == null){
					return;
				}
			    mConnection = new ServiceConnection(){
			    	
					@Override
					public void onServiceConnected(ComponentName arg0, IBinder service) {
						// TODO Auto-generated method stub
						connected = true;
						mService = IRemoteInterface.Stub.asInterface(service);
						
						try {
							Log.i(TAG, mService.getId());
							//mService.registerCallback(cb);
							mService.setPlaylistController(DroolService.PLAYLIST_CONTROLLER_SERVICE);
							Log.i(TAG, "is mp playing " +  mService.isMediaPlayerPlaying());
							//Log.i(TAG, "id = id " + extras.getString("Data").equals(mService.getId()));
							
							
							
							if (!mService.isMediaPlayerPlaying() &&
									!mpState.equals(MyPreferences.MPSTATE_VALUE_PAUSED)){
								
								mService.setId(currentSong, null);
								
								updateNumTimesPlayed(extras.getString(DATA), "Audio");
								mService.registerNowPlayingCallback(npCallback);
								mService.prepareForPlay();
								mService.play();
								but.setText("Pause");	
								SharedPreferences settings = getSharedPreferences(MyPreferences.PREFERENCE_NAME,0);
								SharedPreferences.Editor prefEditor = settings.edit();
								prefEditor.putString(MyPreferences.MPSTATE, MyPreferences.MPSTATE_VALUE_PLAYING);
								prefEditor.commit();
								
								if (pl!= null){
									/*
									if (!pl.isPlaylistCursorOpened())
										try {
											pl.openPlaylistCursor();
											Log.i(TAG, "Playlist opened");
										} catch (InvalidPlaylistException e2) {
											// TODO Auto-generated catch block
											e2.printStackTrace();
										}
									
									*/
									try {
										/*
										try {										
											long row = pl.updatePlaylist(mService.getId());
											Log.i(TAG, "Playlist updated row = " + Long.toString(row));
											pl.cursorRequery();
											pl.setCurrentSongId(mService.getId(), row);
											Log.i(TAG, "current song set");
										} catch (PlaylistException e) {
											
											// TODO Auto-generated catch block
											e.printStackTrace();
										}*/
										pl.addAlreadyPlayedItem(pl.getCurrentSongId());
										settings = getSharedPreferences(MyPreferences.PREFERENCE_NAME,0);							
										prefEditor = settings.edit();
										prefEditor.putInt(MyPreferences.CURRENT_SONG_IN_PLAYLIST, pl.getCurrentSongId());
										prefEditor.putString(MyPreferences.CURRENT_SONG_ID, mService.getId());
										prefEditor.commit();
										
									} catch (NumberFormatException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
								}
							}
								
							
							
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}	

						
					}

					@Override
					public void onServiceDisconnected(ComponentName arg0) {
						// TODO Auto-generated method stub
						Log.i(TAG, "Service disConnected");
						connected = false;
						mService= null;
					}
			    	
			    };
			    
			    if (!mpState.equals(MyPreferences.MPSTATE_VALUE_PLAYING) &&
			    		!mpState.equals(MyPreferences.MPSTATE_VALUE_PAUSED)){
					    bindService(new Intent("com.drool.mediaplayer.services.DroolService"), 
								mConnection, Context.BIND_AUTO_CREATE);
						
						Log.i(TAG, "connected = " + connected);
			    }else{
					
					if (mpState.equals(MyPreferences.MPSTATE_VALUE_PAUSED)){
						try {
							mService.play();
						} catch (RemoteException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						SharedPreferences eSettings = getSharedPreferences(MyPreferences.PREFERENCE_NAME,0);
						SharedPreferences.Editor prefEditor = eSettings.edit();
						prefEditor.putString(MyPreferences.MPSTATE, MyPreferences.MPSTATE_VALUE_PLAYING);
						prefEditor.commit();
						but.setText("Pause");
					}else{
						try {
							mService.pause();
						} catch (RemoteException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}					
						but.setText("Play");
						SharedPreferences eSettings = getSharedPreferences(MyPreferences.PREFERENCE_NAME,0);
						SharedPreferences.Editor prefEditor = eSettings.edit();
						prefEditor.putString(MyPreferences.MPSTATE, MyPreferences.MPSTATE_VALUE_PAUSED);
						prefEditor.commit();
					}
				}
			}
			
		});
		
		
		
		
	}
	
	
	private void updateNumTimesPlayed(String mediaId, String mediaType){
		AudioInfoExtracter aExtracter;
		VideoInfoExtracter vExtracter;
		
		
		if (mediaType.equals("Video")){
			vExtracter  = new VideoInfoExtracter();
			vExtracter.mCursorVideo = managedQuery(vExtracter.mMedia, vExtracter.projection, 
					Audio.Media._ID + " = " + mediaId, null,null);
			Log.i(TAG, Integer.toString(vExtracter.mCursorVideo.getCount()));
			startManagingCursor(vExtracter.mCursorVideo);
			if (vExtracter.mCursorVideo.getCount() == 1){
				vExtracter.mCursorVideo.moveToFirst();
				new WritableDatabase(this).updateNumTimesPlayed(WritableDatabase.VIDEO, 
													vExtracter.mCursorVideo.getString(2));
			}
			vExtracter.mCursorVideo.close();
		}
		else{
			aExtracter  = new AudioInfoExtracter();
			aExtracter.mCursorAudio = managedQuery(aExtracter.mMedia, aExtracter.projection, 
					Audio.Media._ID + " = " + mediaId, null,null);
			Log.i(TAG, Integer.toString(aExtracter.mCursorAudio.getCount()));
			startManagingCursor(aExtracter.mCursorAudio);
			if (aExtracter.mCursorAudio.getCount() == 1){
				aExtracter.mCursorAudio.moveToFirst();
				new WritableDatabase(this).updateNumTimesPlayed(WritableDatabase.AUDIO, 
													aExtracter.mCursorAudio.getString(2));
			}
			aExtracter.mCursorAudio.close();
		}
	}
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		final Cursor aCursor, albumCursor;
		Uri audio = Audio.Media.EXTERNAL_CONTENT_URI;
		Uri album = Audio.Albums.EXTERNAL_CONTENT_URI;
		
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.layalbumbr);
	    
	    extras = getIntent().getExtras();
	    
	    if (extras.getInt(SELECTION, 0) == SELECTION_ALL){	    	
			aCursor = managedQuery(audio,new String [] {
					Audio.Media._ID, Audio.Media.ALBUM, Audio.Media.ALBUM_ID, Audio.Media.ARTIST,
					Audio.Media.DATE_ADDED, Audio.Media.DISPLAY_NAME,Audio.Media.DURATION,
					Audio.Media.TITLE, Audio.Media.TRACK},
					null,null,Audio.Media.DATE_ADDED);
			startManagingCursor(aCursor);
			
			albumCursor = managedQuery(album, new String[] {
					Audio.Albums._ID, Audio.Albums.ALBUM, Audio.Albums.ALBUM_ART},
					null, null, null);
			startManagingCursor(albumCursor);
			
	    	showSongs(aCursor, albumCursor);
	    }else{
	    	Log.i(TAG, "AlbumID " + extras.getString(DATA));
	    	aCursor = managedQuery(audio,new String [] {
					Audio.Media._ID, Audio.Media.ALBUM, Audio.Media.ALBUM_ID, Audio.Media.ARTIST,
					Audio.Media.DATE_ADDED, Audio.Media.DISPLAY_NAME,Audio.Media.DURATION,
					Audio.Media.TITLE, Audio.Media.TRACK},
					Audio.Media.ALBUM_ID + "=" +extras.getString(DATA) ,null,Audio.Media.DATE_ADDED);
			startManagingCursor(aCursor);
			
			albumCursor = managedQuery(album, new String[] {
					Audio.Albums._ID, Audio.Albums.ALBUM, Audio.Albums.ALBUM_ART},
					null, null, null);
			startManagingCursor(albumCursor);
			
	    	//showAlbum(extras.getString(this.DATA));
			showSongs(aCursor, albumCursor);
	    		    	
	    }
	    
	    SharedPreferences settings = getSharedPreferences(MyPreferences.PREFERENCE_NAME,0);
		
		pl = null;
		Log.i(TAG, "settings Playlist " + settings.getLong(MyPreferences.ACTIVE_PLAYLIST, -1));
		if (settings.getLong(MyPreferences.ACTIVE_PLAYLIST, -1) == -1){
			pl = new Playlist(Playlist.PLAYLIST_TYPE_NEW, SongAlbumBrowser.this, "Now Playing");
			Log.i(TAG, "Creating new playlist");
		}else{
			pl = new Playlist(settings.getLong(MyPreferences.ACTIVE_PLAYLIST, -1), SongAlbumBrowser.this);
		}
		
		Log.i(TAG, "Active Playlist " + Long.toString(pl.getActivePlaylist()));
		if (pl.getActivePlaylist() == -1){
			Log.i(TAG, "No active playlist could be attached");
		}
		else{
			SharedPreferences.Editor prefEditor = settings.edit();
			prefEditor.putLong(MyPreferences.ACTIVE_PLAYLIST, pl.getActivePlaylist());					
			prefEditor.commit();
		}
		switch (settings.getInt(MyPreferences.SHUFFLE_STATE, -1)){
			case MyPreferences.SHUFFLE_STATE_VALUE_OFF:
				pl.setShuffle(Playlist.PLAYLIST_SHUFFLE_OFF);
				break;
			case MyPreferences.SHUFFLE_STATE_VALUE_ON:
				pl.setShuffle(Playlist.PLAYLIST_SHUFFLE_ON);
				break;	
			default:
				pl.setShuffle(Playlist.PLAYLIST_SHUFFLE_OFF);
				break;
		}
	
		switch (settings.getInt(MyPreferences.LOOP_STATE, -1)){
			case MyPreferences.LOOP_STATE_VALUE_OFF:
				pl.setLoop(Playlist.PLAYLIST_LOOP_OFF);
				break;
			case MyPreferences.LOOP_STATE_VALUE_ON:
				pl.setLoop(Playlist.PLAYLIST_LOOP_ON);
				break;	
			default:
				pl.setLoop(Playlist.PLAYLIST_LOOP_OFF);
				break;
		}
		
		
		setupPlaylistPanel();
	}
	
	private void setupPlaylistPanel(){
		final Button shuffleBut = (Button)findViewById(R.id.shuffleBut);
		
		shuffleBut.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				Log.i(TAG, "Shuffle setting");
				SharedPreferences settings = getSharedPreferences(MyPreferences.PREFERENCE_NAME,0);
				SharedPreferences.Editor prefEditor = settings.edit();
				
				if (pl.getShuffleState() == Playlist.PLAYLIST_SHUFFLE_OFF){						
					pl.setShuffle(Playlist.PLAYLIST_SHUFFLE_ON);
					shuffleBut.setText("S ON");
					prefEditor.putInt(MyPreferences.SHUFFLE_STATE, MyPreferences.SHUFFLE_STATE_VALUE_ON);
					Log.i(TAG, "Shuffle ON");
				}
				else{
					pl.setShuffle(Playlist.PLAYLIST_SHUFFLE_OFF);
					shuffleBut.setText("S OFF");
					prefEditor.putInt(MyPreferences.SHUFFLE_STATE, MyPreferences.SHUFFLE_STATE_VALUE_OFF);
					Log.i(TAG, "Shuffle OFF");
				}
				prefEditor.commit();
				
			}
		});
		
		
		final Button loopBut = (Button)findViewById(R.id.loopBut);
		loopBut.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Log.i(TAG, "loop setting");
				SharedPreferences settings = getSharedPreferences(MyPreferences.PREFERENCE_NAME,0);
				SharedPreferences.Editor prefEditor = settings.edit();
				
				if (pl.getLoopState() == Playlist.PLAYLIST_LOOP_OFF){						
					pl.setLoop(Playlist.PLAYLIST_LOOP_ON);
					loopBut.setText("L ON");
					prefEditor.putInt(MyPreferences.LOOP_STATE, MyPreferences.LOOP_STATE_VALUE_ON);
					Log.i(TAG, "Loop ON");
				}
				else{
					pl.setLoop(Playlist.PLAYLIST_LOOP_OFF);
					loopBut.setText("L OFF");
					prefEditor.putInt(MyPreferences.LOOP_STATE, MyPreferences.LOOP_STATE_VALUE_OFF);
					Log.i(TAG, "Loop OFF");
				}
				prefEditor.commit();
			}
			
			
			
		});
		
		
		try {
			if (!pl.isPlaylistCursorOpened())
				pl.openPlaylistCursor();
			else
				pl.cursorRequery();
			
			final Cursor playlist = pl.getPlaylist();
			startManagingCursor(playlist);
			Log.i(TAG, "playlist returned with " + Integer.toString(playlist.getCount()) + " items");
			
			
			ListView lv = (ListView) findViewById(R.id.slContentList);
			ListAdapter adapter = new SimpleCursorAdapter(
					SongAlbumBrowser.this, R.layout.layplaylistitem,
				    playlist, new String[] {
						MediaFilesDB.PlaylistItems.PLAYLIST_ITEMS_MEDIA_NAME,
					}, new int[] {R.id.txtPlaylistitem1}
			);
			lv.setAdapter(adapter);
			lv.setOnItemLongClickListener(new OnItemLongClickListener(){

				@Override
				public boolean onItemLongClick(
						AdapterView<?> arg0, View arg1,
						int position, long arg3) {
					// TODO Auto-generated method stub
					playlist.moveToPosition(position);
					final String playlistItemId = playlist.getString(0);
					final int playlistItemIndex = playlist.getPosition();
					AlertDialog alertDialog = new AlertDialog.Builder(SongAlbumBrowser.this).create();
				    alertDialog.setTitle("Delete");
				    alertDialog.setMessage("Do you want to remove this song from the playlist?");
				    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
				    	//final Cursor playlist = pl.getPlaylist();
				      public void onClick(DialogInterface dialog, int which) {
				    	
				    	  
				    	MediaFilesDBHelper myDBHelper = new MediaFilesDBHelper(SongAlbumBrowser.this);
				    	SQLiteDatabase dbWritable = myDBHelper.getWritableDatabase();	
						
						dbWritable.beginTransaction();
						try{
							dbWritable.delete(MediaFilesDB.PlaylistItems.PLAYLIST_ITEMS_TABLE_NAME,
									MediaFilesDB.PlaylistItems._ID + "= " +
									playlistItemId
									, null);
							dbWritable.setTransactionSuccessful();											
						}catch(Exception e){
							Log.e(TAG, "DB Update failed");
							//TODO code rollback - three inserts have to be rollbacked potentially											
							dbWritable.close();				
						}finally{
							dbWritable.endTransaction();
						}
						dbWritable.close();
						
				    	playlist.requery();						    	
				    	Log.i(TAG, "song deleted " + playlistItemId);
				    	pl.removeFromAlreadyPlayed(playlistItemIndex);
				    	pl.adjustAlreadyPlayed(playlistItemIndex);
				    	
				    	if (playlist.getCount() == 0){
				    		Log.i(TAG, "no more songs in playlist");								    		
				    		pl.setCurrentSongId(-1);
				    	}else {
					    	if (playlistItemIndex ==  pl.getCurrentSongId()){
				    			Log.i(TAG, "Current song deleted");
				    			pl.setCurrentSongId(-1);
				    		}else{
				    			if (playlistItemIndex <  pl.getCurrentSongId()){
				    				pl.setCurrentSongId(pl.getCurrentSongId() - 1);
				    			}
				    		}
				    	}
				    	
				    	
				    
				    	  return;
				    	  
				      } }); 
				    alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
					      public void onClick(DialogInterface dialog, int which) {
					        return;
					      } });
					
				    alertDialog.show();
					return true;
				}
				
			});
			
		} catch (InvalidPlaylistException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (PlaylistException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	private void showSongs(final Cursor aCursor, final Cursor albumCursor){		
		ListView lv;
		Log.i("AlbumBrow", "Number of songs for thi album = " + Integer.toString(aCursor.getCount()));
		
		SharedPreferences settings = getSharedPreferences(MyPreferences.PREFERENCE_NAME,0);
		String currentSong = settings.getString(MyPreferences.CURRENT_SONG_ID, "-1");
		
		final SongBrowserAdapter songAdapter = new SongBrowserAdapter(SongAlbumBrowser.this, aCursor, albumCursor,currentSong);
		
		
		
		songAdapter.setFilterQueryProvider(new FilterQueryProvider(){

			@Override
			public Cursor runQuery(CharSequence charseq) {
				Cursor aCursor, albumCursor;
				//Log.i(TAG, "Filterqueryprovider has been run");
				
				
				if (charseq.length() > 0){
					Log.i(TAG, charseq.toString());
					
					albumCursor = managedQuery(Audio.Albums.EXTERNAL_CONTENT_URI,
							new String []{
							Audio.Albums._ID}, Audio.Albums.ALBUM + " like "
							+ "\"%" 
							+ charseq.toString() + "%\"",null,null);
					startManagingCursor(albumCursor);
					String albumSet = "(";
					if (albumCursor.getCount() > 0){
						//ArrayList<Integer> list = new ArrayList<Integer>();
						
						albumCursor.moveToFirst();
						while (!albumCursor.isAfterLast()){
							//list.add(albumCursor.getInt(0));
							
							if (albumCursor.getPosition() >0 && albumCursor.getPosition() != 
								albumCursor.getCount() - 1)
								albumSet = albumSet + ",";
							albumSet = albumSet + albumCursor.getInt(0);
							albumCursor.moveToNext();
						}
					}
					
					albumSet = albumSet + ")";
					
					
					
					aCursor = managedQuery(Audio.Media.EXTERNAL_CONTENT_URI,new String [] {
							Audio.Media._ID, Audio.Media.ALBUM, Audio.Media.ALBUM_ID, Audio.Media.ARTIST,
							Audio.Media.DATE_ADDED, Audio.Media.DISPLAY_NAME,Audio.Media.DURATION,
							Audio.Media.TITLE, Audio.Media.TRACK},
							Audio.Media.TITLE + " like " + "\"%" 
							+ charseq.toString() + "%\"" 
							+ " OR " + 
							Audio.Media.ARTIST + " like " + "\"%" 
							+ charseq.toString() + "%\""
							+ " OR " 
							+ Audio.Media.ALBUM_ID + " in " + albumSet
							,null,Audio.Media.DATE_ADDED);
					startManagingCursor(aCursor);
				}else{
					Log.i(TAG, "resetting the cursor");
					aCursor = managedQuery(Audio.Media.EXTERNAL_CONTENT_URI,new String [] {
							Audio.Media._ID, Audio.Media.ALBUM, Audio.Media.ALBUM_ID, Audio.Media.ARTIST,
							Audio.Media.DATE_ADDED, Audio.Media.DISPLAY_NAME,Audio.Media.DURATION,
							Audio.Media.TITLE, Audio.Media.TRACK},
							null,null,Audio.Media.DATE_ADDED);
					startManagingCursor(aCursor);
				}
					
				//songAdapter.changeCursor(aCursor);
				return aCursor;
				
			}
			
		});

		
		lv = ((ListView)findViewById(R.id.albumListView01));		
		lv.setAdapter(songAdapter);
		
				
		//lv.setSelection(1);
		OnItemClickListener mListListener = new OnItemClickListener() {
			
			@Override
			 public void onItemClick(AdapterView parent, View v, int position, long id)
			 {        // Display a messagebox.        
				 //Toast.makeText(mContext,"You've got an event",Toast.LENGTH_SHORT).show();
					aCursor.moveToPosition(position);
					//Log.i("AlbumBrow", aCursor.getString(5));
					
					Intent intent = new Intent(SongAlbumBrowser.this.getApplicationContext(), 
							MediaPlayerActivity.class);
					
					intent.putExtra(MediaPlayerActivity.TYPE,MediaPlayerActivity.SONG);
					intent.putExtra(MediaPlayerActivity.DATA, aCursor.getString(0));
					intent.putExtra(MediaPlayerActivity.DATA1, aCursor.getString(2));
					startActivity(intent);
					//showVideo(aCursor.getString(0));
			 }
		};
		lv.setOnItemClickListener(mListListener);
		
		
		
		lv.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				
				aCursor.moveToPosition(position);
				Log.i(TAG, "Long click registered pos " + Integer.toString(position) + " " + aCursor.getString(5));
				
				try {
					pl.updatePlaylist(aCursor.getString(0));
					Toast.makeText(SongAlbumBrowser.this, "Song added to the playlist",Toast.LENGTH_SHORT).show();
				} catch (InvalidPlaylistException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				/*Intent intent = new Intent(SongAlbumBrowser.this.getApplicationContext(), 
						PlaylistSelectActivity.class);
					startActivity(intent);
				*/
				/*
				intent.putExtra(MediaPlayerActivity.TYPE,MediaPlayerActivity.SONG);
				intent.putExtra(MediaPlayerActivity.DATA, aCursor.getString(0));
				intent.putExtra(MediaPlayerActivity.DATA1, aCursor.getString(2));
				*/
				return true;
			}
			
		});
	}
	
	/*
	private void showAlbum(String albumId){
		
		int selectedView = 0;
		
		setContentView(R.layout.layalbumbrbig);
		
		Uri album = Audio.Albums.EXTERNAL_CONTENT_URI;
		Cursor albumCursor = managedQuery(album, new String [] {
				Audio.Albums._ID, Audio.Albums.ALBUM_ART,
				Audio.Albums.ALBUM, Audio.Albums.ARTIST,
				Audio.Albums.FIRST_YEAR},
				//Audio.Albums._ID + " = " + albumId,
				null,
				null,Audio.Albums.ALBUM);
		
		startManagingCursor(albumCursor);
		
    	if (albumCursor.getCount() > 0){
    		int position = extras.getInt(DATA1);
    		
    		albumCursor.moveToPosition(position - 1);
    		
    		if (albumCursor.getPosition() == -1){
    			albumCursor.moveToPosition(position);
    			selectedView = 1;
    		}
    		else
    			selectedView = 2;
    		
    		Log.i("AlbumBrow", "Setting up album " + albumCursor.getString(2));
    		setupLayAlbum1(albumCursor,albumCursor.getPosition());
    		albumCursor.moveToNext();
    		if (!albumCursor.isAfterLast()){
    			Log.i("AlbumBrow", "Setting up album " + albumCursor.getString(2));
    			setupLayAlbum2(albumCursor,albumCursor.getPosition());
    			albumCursor.moveToNext();
    			if (!albumCursor.isAfterLast()){
    				Log.i("AlbumBrow", "Setting up album " + albumCursor.getString(2));
    				setupLayAlbum3(albumCursor,albumCursor.getPosition());
    				albumCursor.moveToNext();
    			}
    			else
    				Log.i("AlbumBrow", "End of cursor, setting of view3 failed");
    		}
    		else
    			Log.i("AlbumBrow", "End of cursor, setting of view2 failed");
    	
    		ViewFlipper flipper = (ViewFlipper)findViewById(R.id.ViewFlipper01);
    		if (selectedView == 2)
    			flipper.showNext();
    	}
	}
	
	*/
	
	private void setupLayAlbum1(Cursor aCursor, int cursorPos){
		ImageView albumArt = (ImageView)findViewById(R.id.albumArt1);
		TextView albumName = (TextView)findViewById(R.id.albumName1);
		ListView songList = (ListView)findViewById(R.id.songList1);
		
		Uri audio = Audio.Media.EXTERNAL_CONTENT_URI;
		
		aCursor.moveToPosition(cursorPos);
		
		if (aCursor.getString(1) != null){
			albumArt.setImageURI(Uri.parse(aCursor.getString(1)));
		}
		else
			albumArt.setImageResource(R.drawable.musicdelta);
		albumName.setText(aCursor.getString(2));
		
		Cursor audioCursor = managedQuery(audio,new String [] {
				Audio.Media._ID, Audio.Media.ALBUM, Audio.Media.ALBUM_ID, Audio.Media.ARTIST,
				Audio.Media.DATE_ADDED, Audio.Media.DISPLAY_NAME,Audio.Media.DURATION,
				Audio.Media.TITLE, Audio.Media.TRACK},
				Audio.Media.ALBUM_ID + " = " + aCursor.getShort(0),
				//null,
				null,null);
		
    	startManagingCursor(audioCursor);
		
	}
	
	private void setupLayAlbum2(Cursor aCursor, int cursorPos){
		ImageView albumArt = (ImageView)findViewById(R.id.albumArt2);
		TextView albumName = (TextView)findViewById(R.id.albumName2);
		ListView songList = (ListView)findViewById(R.id.songList2);
		
		Uri audio = Audio.Media.EXTERNAL_CONTENT_URI;
		
		aCursor.moveToPosition(cursorPos);
		
		if (aCursor.getString(1) != null){
			albumArt.setImageURI(Uri.parse(aCursor.getString(1)));
		}
		else
			albumArt.setImageResource(R.drawable.musicdelta);
		albumName.setText(aCursor.getString(2));
		
		Cursor audioCursor = managedQuery(audio,new String [] {
				Audio.Media._ID, Audio.Media.ALBUM, Audio.Media.ALBUM_ID, Audio.Media.ARTIST,
				Audio.Media.DATE_ADDED, Audio.Media.DISPLAY_NAME,Audio.Media.DURATION,
				Audio.Media.TITLE, Audio.Media.TRACK},
				Audio.Media.ALBUM_ID + " = " + aCursor.getShort(0),
				//null,
				null,null);
		
    	startManagingCursor(audioCursor);
		
	}
	
	private void setupLayAlbum3(Cursor aCursor, int cursorPos){
		ImageView albumArt = (ImageView)findViewById(R.id.albumArt3);
		TextView albumName = (TextView)findViewById(R.id.albumName3);
		ListView songList = (ListView)findViewById(R.id.songList3);
		
		Uri audio = Audio.Media.EXTERNAL_CONTENT_URI;
		
		aCursor.moveToPosition(cursorPos);
		
		if (aCursor.getString(1) != null){
			albumArt.setImageURI(Uri.parse(aCursor.getString(1)));
		}
		else
			albumArt.setImageResource(R.drawable.musicdelta);
		albumName.setText(aCursor.getString(2));
		
		Cursor audioCursor = managedQuery(audio,new String [] {
				Audio.Media._ID, Audio.Media.ALBUM, Audio.Media.ALBUM_ID, Audio.Media.ARTIST,
				Audio.Media.DATE_ADDED, Audio.Media.DISPLAY_NAME,Audio.Media.DURATION,
				Audio.Media.TITLE, Audio.Media.TRACK},
				Audio.Media.ALBUM_ID + " = " + aCursor.getShort(0),
				//null,
				null,null);
		
    	startManagingCursor(audioCursor);
		
	}
	
}
